////////////////////////////////////////////////////////////////////////////////

#include "server_engine.h"

////////////////////////////////////////////////////////////////////////////////

#include <cassert>
#include <cmath>

////////////////////////////////////////////////////////////////////////////////


ServerEngine::ServerEngine() : m_KeyFlags (0x0)
{
}


////////////////////////////////////////////////////////////////////////////////


ServerEngine::~ServerEngine()
{
}


////////////////////////////////////////////////////////////////////////////////


void ServerEngine::ProcessLogic()
{
    ProcessCollisions();
    ProcessMovement();
}


////////////////////////////////////////////////////////////////////////////////


void ServerEngine::ProcessCollisions()
{
    //Not implemented yet
}


////////////////////////////////////////////////////////////////////////////////


void ServerEngine::ProcessMovement()
{
    // Set players velocity
    SetVelocity();

    // Move player
    m_Battlefield.GetPlayer().Move();
}


////////////////////////////////////////////////////////////////////////////////


void ServerEngine::SetVelocity()
{
    // Get current velocity
    STM::Velocity velocity(0, 0);
    double absoluteVelocity = m_Battlefield.GetPlayer().GetAbsoluteVelocity();

    if ( m_KeyFlags & W_KEY )
        // Move up
        velocity.second -= absoluteVelocity;

    if ( m_KeyFlags & S_KEY )
        // Move down
        velocity.second += absoluteVelocity;

    if ( m_KeyFlags & A_KEY )
        // Move left
        velocity.first -= absoluteVelocity;

    if ( m_KeyFlags & D_KEY )
        // Move right
        velocity.first += absoluteVelocity;

    // Diagonal movement
    if (velocity.first != 0 && velocity.second != 0)
    {
        const double angle = 45;
        velocity.first *= sin(angle);
        velocity.second *= cos(angle);
    }

    // Set new velocity
    m_Battlefield.GetPlayer().SetVelocity(velocity);
}


////////////////////////////////////////////////////////////////////////////////


void ServerEngine::ProcessEvents( unsigned int _keyFlags,
                                  STM::Position _mousePosition )
{
    m_KeyFlags = _keyFlags;
}


////////////////////////////////////////////////////////////////////////////////


void ServerEngine::FetchDrawData( STM::ScrPositions& _players,
                                  STM::ScrPositions& _bullets )
                                 
{
    STM::Position doublePos = m_Battlefield.GetPlayer().GetPosition();
    STM::ScrPosition pos;
    pos.first = static_cast<short>(doublePos.first);
    pos.second = static_cast<short>(doublePos.second);

    _players.push_back(pos);

    const Battlefield::Bullets& bullets = m_Battlefield.GetBullets();

    for (Battlefield::Bullets::const_iterator it = bullets.begin(); it != bullets.end(); ++it)
    {
        doublePos = it->GetPosition();
        pos.first = static_cast<short>(doublePos.first);
        pos.second = static_cast<short>(doublePos.second);

        _bullets.push_back(pos);
    }
}


////////////////////////////////////////////////////////////////////////////////
